//
//  PostTweetViewController.m
//  PostTweet
//

#import "PostTweetViewController.h"

@implementation PostTweetViewController

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	
	UIAlertView *myAlert = [[UIAlertView alloc]
							initWithTitle:@"Error"
							message:[error localizedDescription]
							delegate:nil
							cancelButtonTitle:@"OK"
							otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	
	// 결과를 콘솔에 기록한다.
	NSString *result = [[NSString alloc]
						initWithBytes:[receivedData bytes]
						length:[receivedData length]
						encoding:NSASCIIStringEncoding];
	NSLog(@"%@", result);
	[result release];
	
	// 성공했다고 가정하고,
	UIAlertView *myAlert = [[UIAlertView alloc]
							initWithTitle:@"Success"
							message:@"Tweet posted successfully"
							delegate:nil
							cancelButtonTitle:@"OK"
							otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)chg {
	
	NSString *userName = @"appler";
	NSString *userPassword = @"your_password";
	if ([chg previousFailureCount] == 0) {
		NSURLCredential *newCredential;
		newCredential = [NSURLCredential credentialWithUser:userName
												   password:userPassword
												persistence:NSURLCredentialPersistenceNone];
		[[chg sender] useCredential:newCredential forAuthenticationChallenge:chg];
	}
	else {
		[[chg sender] cancelAuthenticationChallenge:chg];
	}
}

- (void)tweetClick:(id)sender {
	[receivedData setLength:0];
	
	//
	NSString *twitterURL = @"http://twitter.com/statuses/update.xml";
	NSURL *url = [NSURL URLWithString:twitterURL];
	NSMutableURLRequest *req = [[NSMutableURLRequest alloc]
								initWithURL:url
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
								timeoutInterval:30.0];
	[req setHTTPMethod:@"POST"];
	NSString *twitterBody = [NSString stringWithFormat:@"status=%@", myTextField.text];
	[req setHTTPBody:[twitterBody dataUsingEncoding:NSISOLatin1StringEncoding]];
	
	NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
	[req release];
	[conn release];
}

- (void)createUI {
	
	CGRect textRect = CGRectMake(10, 10, 230, 32);
	myTextField = [[UITextField alloc] initWithFrame:textRect];
	myTextField.borderStyle = UITextBorderStyleRoundedRect;
	[self.view addSubview:myTextField];
	
	CGRect buttonRect = CGRectMake(250, 10, 60, 32);
	myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[myButton setFrame:buttonRect];
	[myButton setTitle:@"Tweet" forState:UIControlStateNormal];
	[myButton addTarget:self action:@selector(tweetClick:)
	   forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:myButton];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	[self createUI];
	receivedData = [[NSMutableData alloc] initWithData:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	
	[myTextField release];
	[myButton release];
	
    [super dealloc];
}

@end
